package edu.yuhf.dao;

import com.yuhf.dbutils.JdbcTemplate;
import com.yuhf.dbutils.iface.RowMapper;
import com.yuhf.page.Page;
import edu.yuhf.dao.iface.StudentDao;
import edu.yuhf.domain.Student;
import edu.yuhf.domain.StudentMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yuhf
 */
public class StudentJdbcDaoImpl implements StudentDao {

    @Override
    public List<Student> queryStudentAll(Page<Student> page) {
        String sql="select * from student_info limit ?,?";
        return JdbcTemplate.query(sql,new StudentMapper(),(page.getCurrentPage()-1)*page.getRowNumber(),page.getRowNumber());
    }

    @Override
    public int queryStudentAllTotalRow() {
        String sql="select count(*) from student_info";
        return JdbcTemplate.getTotalRow(sql);
    }

    private StudentJdbcDaoImpl(){}

    @Override
    public List<Student> queryStudentByName(String name) {
        String sql="select *from student_info where name=?";
        return JdbcTemplate.query(sql,new StudentMapper(),name);
    }

    public static StudentDao factory(){
        return new StudentJdbcDaoImpl();
    }
    /**
     * 查询某个学院中的所有学通过
     */
    @Override
    public List<Map<String, Object>> queryStudentByCollegeId(int collegeId) {
        String sql="select si.id,si.sno,si.name,si.sex,si.birthday,c.college_name collegeNam,c.college_address collegeAddress from student_info si inner join college c on si.college_id=c.id where c.id=?";
        var list = JdbcTemplate.query(sql,collegeId);
        return list;
    }

    @Override
    public int deleteStudent(int id) {
        String sql = "delete from student_info where id=?";
        return JdbcTemplate.update(sql,id);
    }
}
